package api

import (
	"RESTfulAPI/basic/config"
	__ "RESTfulAPI/basic/proto"
	"RESTfulAPI/handler/request"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func ListPage(c *gin.Context) {
	var req request.ListPageReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数错误",
			"data": err.Error(),
		})
		return
	}
	r, err := config.UserClient.ListPage(c, &__.ListPageReq{
		Page:     int32(req.Page),
		PageSize: int32(req.PageSize),
	})
	if err != nil {
		log.Printf("ListPage error:%v", err)
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": r.Code,
		"msg":  r.Msg,
		"data": r,
	})

}

func DetailGoods(c *gin.Context) {
	var req request.DetailGoodsReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数错误",
			"data": err.Error(),
		})
		return
	}
	r, err := config.UserClient.DetailGoods(c, &__.DetailGoodsReq{
		Id: int32(req.Id),
	})
	if err != nil {
		log.Printf("DetailGoods error:%v", err)
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": r.Code,
		"msg":  r.Msg,
		"data": r,
	})
}

func ShowGoods(c *gin.Context) {
	var req request.ShowGoodsReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数错误",
			"data": err.Error(),
		})
		return
	}
	r, err := config.UserClient.ShowGoods(c, &__.ShowGoodsReq{
		Id: int32(req.Id),
	})
	if err != nil {
		log.Printf("ShowGoods error:%v", err)
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": r.Code,
		"msg":  r.Msg,
		"data": r,
	})
}
